
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClienteLeer implements Runnable {
	private String serverHostname = new String("localhost");
	private int port = 5000;
	private String nombre = "clienteSinNombre";

	public ClienteLeer(String serverHostname, int port, String nombre) {
		super();
		this.serverHostname = serverHostname;
		this.port = port;
		this.nombre = nombre;
		
		
	}

	public ClienteLeer() {
	}

	public void run() {

	
		Socket s = null;
		BufferedReader br = null;

		try {
			s = new Socket(serverHostname, port);

			br = new BufferedReader(new InputStreamReader(
					s.getInputStream()));

		} catch (UnknownHostException e) {
			System.err.println("No conozco el servidor: " + serverHostname);
			System.exit(1);
		} catch (IOException e) {
			System.err.println("No consigo entrar " + "al servidor "
					+ serverHostname);
			System.exit(1);
		}

		try {
			
			boolean flag = true;
			while(flag) {
				String textoLeido = br.readLine();
				if (textoLeido.equals(nombre + ": Bye.")) {
					flag=false;
					Thread.interrupted();
										
				} else
					System.out.println(textoLeido);
			} 
		} catch (IOException e) {
		
			e.printStackTrace();
		}

		try {
			br.close();
			s.close();
		} catch (IOException e) {
		
			e.printStackTrace();
		}

	}
}
